#include "mypushbutton.h"
#include <QPixmap>
#include <QDebug>
#include <QSize>
#include <QPropertyAnimation>
#include <QRect>
#include <QEasingCurve>



MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
{

}
MyPushButton::MyPushButton(QString normalImg,QString pressImg){
    normalImgPath = normalImg;
    pressedImgPath = pressImg;
    QPixmap pixmap;
    //判断是否能正常加载图片，若不能则提示用户
    bool ret = pixmap.load(normalImgPath);
    if(!ret){
       qDebug() << normalImg <<"加载图片失败" ;

    }
    //设置图片固定尺寸
    this->setFixedSize(pixmap.width(),pixmap.height());
    //设置不规则图片的样式表
    this->setStyleSheet("QPushButton{border:0px}");
    //设置图标
    this->setIcon(pixmap);
    //设置图标大小
    this->setIconSize(QSize(pixmap.width(),pixmap.height()));


}
void MyPushButton::zoom1(){
    //创建动画对象
    QPropertyAnimation *animation1 = new QPropertyAnimation(this,"geometry");
    //设置时间间隔，单位毫秒
    animation1->setDuration(200);
    //创建其实位置
    animation1->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
     animation1->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置缓和曲线,QEasingCurve::OutBounce 为弹跳效果
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画
    animation1->start();

}

void MyPushButton::zoom2(){
    //创建动画对象
    QPropertyAnimation *animation1 = new QPropertyAnimation(this,"geometry");
    //设置时间间隔，单位毫秒
    animation1->setDuration(200);
    //创建其实位置
    animation1->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
     animation1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置缓和曲线,QEasingCurve::OutBounce 为弹跳效果
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画
    animation1->start();
}


void MyPushButton::mousePressEvent(QMouseEvent *e){
    if(pressedImgPath!=""){//选中路径不为空，显示选中图片
        QPixmap pixmap;
        bool ret = pixmap.load(pressedImgPath);
        if(!ret)
        {
            qDebug() << pressedImgPath << "加载图片失败!";
        }
        //设置图标
        this->setFixedSize(pixmap.width(),pixmap.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));

    }
    return QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e){
    if(normalImgPath != "") //选中路径不为空，显示选中图片
        {
            QPixmap pixmap;
            bool ret = pixmap.load(normalImgPath);
            if(!ret)
            {
                qDebug() << normalImgPath << "加载图片失败!";
            }
            this->setFixedSize( pixmap.width(), pixmap.height() );
            this->setStyleSheet("QPushButton{border:0px;}");
            this->setIcon(pixmap);
            this->setIconSize(QSize(pixmap.width(),pixmap.height()));
        }
    return QPushButton::mouseReleaseEvent(e);
}
